package com.thinkit.bigdata.web.service.impl.bus;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thinkit.bigdata.core.feature.orm.mybatis.Page;
import com.thinkit.bigdata.core.util.httpUtil.HttpUtil;
import com.thinkit.bigdata.web.dao.bus.BusModelMapper;
import com.thinkit.bigdata.web.dao.bus.BusModelRuleMapper;
import com.thinkit.bigdata.web.dao.bus.BusModelTypeMapper;
import com.thinkit.bigdata.web.model.bus.*;
import com.thinkit.bigdata.web.service.bus.BusModelService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hccl on 2018/9/10.
 * modify by lichangyuan 2018年9月27日
 */
@Service
@Transactional
public class BusModelServiceImpl implements BusModelService {

    private static final Logger log = LoggerFactory.getLogger(BusModelServiceImpl.class);

    private static final ObjectMapper mapper = new ObjectMapper();

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
    @Resource
    private BusModelMapper busModelMapper;

    @Resource
    private BusModelRuleMapper busModelRuleMapper;

    @Resource
    private BusModelTypeMapper busModelTypeMapper;

    @Override
    public BusModel selectByPrimaryKey(Long id) {
        return busModelMapper.selectByPrimaryKey(id);
    }

    @Override
    public BusModel selectById(Long id){
        ModelQueryParam modelInfoOV = new ModelQueryParam();
        modelInfoOV.setId(id);
        List<BusModel> models = busModelMapper.selectByConditionAndMutilTable(modelInfoOV);
        if(models!=null&&models.size()>0){
            return models.get(0);
        }
        return null;
    }

    @Override
    public int insert(BusModel busModel) {
        int insert = busModelMapper.insert(busModel);
        BusModelExample example = new BusModelExample();
        example.createCriteria().andNameEqualTo(busModel.getName())
        .andProvincecodeEqualTo(busModel.getProvincecode())
        .andTypeidEqualTo(busModel.getTypeid());
        List<BusModel>  list = busModelMapper.selectByExample(example);
        if(list!=null&&list.size()>0){
            busModel.setId(list.get(0).getId());
        }
        //插入规则信息
        for (BusModelRule rule : busModel.getModelrulelist()) {
            rule.setId(null);
            rule.setModelid(busModel.getId());
            busModelRuleMapper.insert(rule);
        }
        //分类下添加模型后,更新分类为叶子节点.
        BusModelType busModelType = new BusModelType();
        busModelType.setId(busModel.getTypeid());
        busModelType.setLeaf(1);
        busModelTypeMapper.updateByPrimaryKeySelective(busModelType);
        return insert;
    }

    @Override
    public int insertSelective(BusModel record) {
        return busModelMapper.insertSelective(record);
    }

    @Override
    public int countByExample(BusModelExample example) {
        return busModelMapper.countByExample(example);
    }

    @Override
    public List<BusModel> selectByExample(Page<BusModel> page, BusModelExample example) {
        return busModelMapper.selectByExample(page, example);
    }

    @Override
    public List<BusModel> selectByExample(BusModelExample example) {
        return busModelMapper.selectByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        BusModelRuleExample ruleExample = new BusModelRuleExample();
        ruleExample.createCriteria().andModelidEqualTo(id);
        busModelRuleMapper.deleteByExample(ruleExample);
        return busModelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByExample(BusModelExample example, List<Long> modelIdList) {
        //删除规则信息
        BusModelRuleExample ruleExample = new BusModelRuleExample();
        BusModelRuleExample.Criteria criteria = ruleExample.createCriteria();
        criteria.andModelidIn(modelIdList);
        busModelRuleMapper.deleteByExample(ruleExample);
        return busModelMapper.deleteByExample(example);
    }

    @Override
    public int updateByExampleSelective(BusModel record, BusModelExample example) {

        return busModelMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByPrimaryKey(BusModel record) {
        return busModelMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(BusModel record) {
        BusModelRuleExample busModelRuleExample = new BusModelRuleExample();
        busModelRuleExample.createCriteria().andModelidEqualTo(record.getId());
        if (record.getModelrulelist() == null || record.getModelrulelist().isEmpty()) {
            busModelRuleMapper.deleteByExample(busModelRuleExample);
        } else {
            busModelRuleMapper.deleteByExample(busModelRuleExample);
            for (BusModelRule rule : record.getModelrulelist()) {
                rule.setModelid(record.getId());
                busModelRuleMapper.insert(rule);
            }
        }
        return busModelMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<BusModel> selectByConditionAndPagenationAndMutilTable(HashMap<String, Object> params) {
        return busModelMapper.selectByConditionAndPagenationAndMutilTable(params);
    }

    @Override
    public List<BusModel> selectByConditionAndPagenationAndMutilTable(Page<BusModel> page, HashMap<String, Object> params) {
        List<BusModel> busModels = busModelMapper.selectByConditionAndPagenationAndMutilTable(page, params);
        for (BusModel record : busModels) {
            BusModelRuleExample example = new BusModelRuleExample();
            example.createCriteria()
                    .andModelidEqualTo(record.getId());
            List<BusModelRule> busModelRules = busModelRuleMapper.selectByExample(example);
            record.setModelrulelist(busModelRules);
        }

        return busModels;
    }

    @Override
    public List<BusModel> selectByPrimaryKeyWithCopy(List list) {
        return busModelMapper.selectByPrimaryKeyWithCopy(list);
    }

    @Override
    public int deleteByTypeId(Long typeId) {
        //删除规则信息
        BusModelExample example = new BusModelExample();
        example.createCriteria()
                .andTypeidEqualTo(typeId);
        //删除模型,删除规则
        List<BusModel> busModels = busModelMapper.selectByExample(example);
        for (BusModel record : busModels) {
            busModelRuleMapper.deleteByModelId(record.getId());
        }

        return busModelMapper.deleteByTypeId(typeId);
    }

    @Override
    public List<BusModel> selectByPage(Page<BusModel> page, HashMap<String, Object> params) {
        List<BusModel> busModels = busModelMapper.selectByPage(page, params);
        addModelRuleAll(busModels);
        return busModels;
    }

    @Override
    public List<BusModel> select_model_all(HashMap<String, Object> params) {
        return busModelMapper.selectByPage(params);
    }

    private void addModelRuleAll(List<BusModel> busModels){
        if(busModels!=null){
            for (BusModel record : busModels) {
                BusModelRuleExample example = new BusModelRuleExample();
                example.createCriteria()
                        .andModelidEqualTo(record.getId());
                List<BusModelRule> busModelRules = busModelRuleMapper.selectByExample(example);
                record.setModelrulelist(busModelRules);
            }
        }
    }

    @Override
    public List<BusModel> select_model_rule_all(HashMap<String, Object> params) {
        //查询所有模型
        List<BusModel> busModels = busModelMapper.selectByPage(params);
        addModelRuleAll(busModels);
        return busModels;
    }

    /**
     * @param params
     * @param url
     * @return
     * @throws IOException
     */
    @Override
    public HSSFWorkbook createExcel(HashMap<String, Object> params, String url) throws IOException {
        List<BusModel> busModels = select_model_rule_all(params);
        HSSFWorkbook hssfSheet = createHSSFSheet(busModels, url);
        return hssfSheet;
    }

    @Override
    public List<Map<String, Object>> query_modelTask_status(List<Long> ids) {
        return busModelMapper.query_modelTask_status(ids);
    }

    @Override
    public List<Object> query_model_status(List<Long> ruleIds) {
        ArrayList<Object> result_list = new ArrayList<>();
        for (Long ruleId : ruleIds) {
            BusModelRuleExample example = new BusModelRuleExample();
            example.createCriteria().andRuleidEqualTo(ruleId);
            List<BusModelRule> ruleList = busModelRuleMapper.selectByExample(example);//根据ruleId去查询
            HashMap<String, Object> result_map = new HashMap<>();
            ArrayList<Object> model_list = new ArrayList<>();
            for (BusModelRule rule : ruleList) {
                HashMap<String, Object> model_map = new HashMap<>();
                BusModel model = busModelMapper.selectByPrimaryKey(rule.getModelid());
                if(model == null || model.getId() == null){
                    continue;
                }
                model_map.put("modelId", model.getId());
                model_map.put("modelName", model.getName());
                model_map.put("status", model.getPoststatus());
                model_list.add(model_map);
            }
            result_map.put("ruleId", ruleId);
            result_map.put("models", model_list);
            result_list.add(result_map);
        }
        return result_list;
    }

    private CellStyle createCellStyle(HSSFWorkbook wk){
        CellStyle headerStyle = wk.createCellStyle();
        // 居中 //垂直居中 //下边框 //左边框 //上边框 //右边框
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headerStyle.setBorderBottom(BorderStyle.THIN);
        headerStyle.setBorderLeft(BorderStyle.THIN);
        headerStyle.setBorderTop(BorderStyle.THIN);
        headerStyle.setBorderRight(BorderStyle.THIN);
        return headerStyle;
    }

    /**
     * @param model_list 模型列表
     * @param url       第三方获取规则信息的接口
     * @throws IOException
     */
    public HSSFWorkbook createHSSFSheet(List<BusModel> model_list, String url) throws IOException {

        HSSFWorkbook wk = new HSSFWorkbook();
        // 创建一张工作表
        HSSFSheet sheet = wk.createSheet();
        // 列自适应宽度sheet.autoSizeColumn(col_num)需要在赋值完数据后使用

        // 列头单元格样式,上下左右边框
        CellStyle headerStyle = createCellStyle(wk);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        //列头字体样式,等线字体,列头加粗,11号字体  //加粗
        Font headerFont = wk.createFont();
        headerFont.setFontHeightInPoints((short) 11);
        headerFont.setFontName("等线");
        headerFont.setBold(true);
        headerStyle.setFont(headerFont);

        //普通单元格样式1
        CellStyle cellStyle1 = createCellStyle(wk);
        //普通字体样式1,等线字体,11号字体
        Font cellFont1 = wk.createFont();
        cellFont1.setFontHeightInPoints((short) 11);
        cellFont1.setFontName("等线");
        cellStyle1.setFont(cellFont1);

        //1. 创建第0行
        HSSFRow row = sheet.createRow(0);
        String[] head = new String[]{"序号", "省份", "分类", "模型名称", "模型分数","状态","测检状态", "创建人", "创建时间", "上线时间",
                "最后更新时间", "模型备注", "规则分类", "规则名称", "规则属性", "致命性", "单次扣分", "扣分上限", "子规则序号",
                "子规则", "子规则内容", "子规则位置", "创建人", "规则创建时间", "规则上线时间", "规则最后更新时间", "规则备注"};
        log.info("表头长度:{}", head.length);
        for (int i = 0; i < head.length; i++) {
            // 表头赋值//设置表头样式
            row.createCell(i).setCellValue(head[i]);
            row.getCell(i).setCellStyle(headerStyle);
        }
        //2. 遍历: 模型集合,赋值
        // 全局记录
        int row_num = 1;
        for (int i = 0; i < model_list.size(); i++) {
            //2.1. 获取一条模型信息
            BusModel model = model_list.get(i);
            if(model==null){
                continue;
            }
            List<BusModelRule> local_rule_list = model.getModelrulelist();
            //2.2. 判断: 本地规则为空, 写入一行数据
            if (local_rule_list==null || local_rule_list.size() == 0) {
                row_num = create_one_row(sheet, cellStyle1, model, row_num, i);
                continue;
            }
            //2.3. 请求: 获取第三方项目获取规则条目数,异常处理: 返回空ArrayList
            ArrayList<Long> ruleIds = new ArrayList<>();
            for (int x = 0; x < local_rule_list.size(); x++) {
                if(local_rule_list.get(x)!=null){
                    ruleIds.add(local_rule_list.get(x).getRuleid());
                }
            }
            List<Modelrulelist> remote_rule_list = post_model_rule(url, ruleIds);
            //2.4. 判断: 远程规则为空, 写入本地规则数据, 不写入远程详细信息
            if (remote_rule_list.size() == 0) {
                //创建多行,写入模型信息
                row_num = create_multi_row(sheet, cellStyle1, model, row_num, i);
                continue;
            }
            //2.5. 遍历: Modelrulelist
            // 一个模型(bus_model)的所有规则详细信息(item)的条目
            int item_sum = 0;
            for (int x = 0; x < remote_rule_list.size(); x++) {
                // 2.5.1. 控制: 获取远程规则(Modelrulelist)
                Modelrulelist modelrulelist = remote_rule_list.get(x);
                int item_list_size = 1;
                if(modelrulelist!=null&& modelrulelist.getItems()!=null){
                    //一条远程规则(Modelrulelist)的规则的详细信息(items)
                    item_list_size = modelrulelist.getItems().size();
                }
                if(item_list_size==0){
                    item_list_size = 1;
                }
                for (int a = 0; a < item_list_size; a++, item_sum++, row_num++) {
                    //2.5.1.1. 控制: 获取规则详细信息 //创建sheet行对象
                    HSSFRow rowX = sheet.createRow(row_num);
                    /*"序号", "省份", "分类", "模型名称", "模型分数","状态","测检状态", "创建人", "创建时间", "上线时间","最后更新时间", "模型备注"*/
                    rowX.createCell(0).setCellValue(i + 1);
                    rowX.createCell(1).setCellValue(model.getAreaname() == null ? "" : model.getAreaname());
                    rowX.createCell(2).setCellValue(model.getModeltypename() == null ? "" : model.getModeltypename());
                    rowX.createCell(3).setCellValue(model.getName() == null ? "" : model.getName());
                    rowX.createCell(4).setCellValue(model.getFullscore() == null ? "" : model.getFullscore().toString());
                    rowX.createCell(5).setCellValue(
                            (model.getPoststatus()==null || model.getPoststatus() == 0) ? "未上线" : "已上线");
                    rowX.createCell(6).setCellValue(
                            (model.getTesttimes()==null||model.getTesttimes() == 0) ? "未测检" : "已测检");
                    rowX.createCell(7).setCellValue(model.getUsername() == null ? "" : model.getUsername());
                    rowX.createCell(8).setCellValue(model.getCreatetime() == null ? "" : sdf.format(model.getCreatetime()));
                    rowX.createCell(9).setCellValue(model.getOnsettime() == null ? "" : sdf.format(model.getOnsettime()));
                    rowX.createCell(10).setCellValue(model.getLastupdatetime() == null ? "" : sdf.format(model.getLastupdatetime()));
                    rowX.createCell(11).setCellValue(model.getNote() == null ? "" : model.getNote());
                    //"规则分类", "规则名称", "规则属性", "致命性", "单次扣分", "扣分上限",
                    BusModelRule modelRule =  local_rule_list.get(x);
                    rowX.createCell(12).setCellValue(modelRule.getRuletypename() == null ? "" : modelRule.getRuletypename());
                    rowX.createCell(13).setCellValue(modelRule.getRulename() == null ? "" : modelRule.getRulename());
                    rowX.createCell(14).setCellValue(
                            (modelRule.getUserfor() == null||modelRule.getUserfor() == 0 )? "系统" : "人工");
                    if(modelRule.getSignscore()==null||modelRule.getSignscore()!=1){
                        rowX.createCell(15).setCellValue(
                                modelRule.getFateful()==null||modelRule.getFateful() == 0 ? "非致命性" : "致命性");
                    }else{
                        rowX.createCell(15).setCellValue("");
                    }
                    rowX.createCell(16).setCellValue(modelRule.getScorepertime() == null ? "" : modelRule.getScorepertime().toString());
                    rowX.createCell(17).setCellValue(modelRule.getMaxscore() == null ? "" : modelRule.getMaxscore().toString());
                    //规则详细信息 "子规则序号","子规则", "子规则内容", "子规则位置"
                    if(modelrulelist!=null&&modelrulelist.getItems()!=null&&modelrulelist.getItems().size()>a){
                        Items items = modelrulelist.getItems().get(a);
                        if(items!=null){
                            rowX.createCell(18).setCellValue(items.getNumber() == null ? "" : items.getNumber());
                            rowX.createCell(19).setCellValue(items.getType() == null ? "" : items.getType());
                            rowX.createCell(20).setCellValue(items.getContent() == null ? "" : items.getContent());
                            rowX.createCell(21).setCellValue(items.getPosition() == null ? "" : items.getPosition());
                        }else{
                            for(int ri=18;ri<22;ri++){
                                rowX.createCell(ri).setCellValue("");
                            }
                        }
                    }else{
                        for(int ri=18;ri<22;ri++){
                            rowX.createCell(ri).setCellValue("");
                        }
                    }
                    //规则其他信息，"创建人", "规则创建时间", "规则上线时间", "规则最后更新时间", "规则备注"
                    rowX.createCell(22).setCellValue(modelrulelist!=null&&modelrulelist.getAuthor() == null ? "" : modelrulelist.getAuthor());
                    rowX.createCell(23).setCellValue(modelrulelist!=null&&modelrulelist.getCreate_time() == null ? "" : sdf.format(modelrulelist.getCreate_time()));
                    rowX.createCell(24).setCellValue(modelrulelist!=null&&modelrulelist.getPublish_time() == null ? "" : sdf.format(modelrulelist.getPublish_time()));
                    rowX.createCell(25).setCellValue(modelrulelist!=null&&modelrulelist.getEnd_time() == null ? "" : sdf.format(modelrulelist.getEnd_time()));
                    rowX.createCell(26).setCellValue(modelrulelist!=null&&modelrulelist.getNotes() == null ? "" : modelrulelist.getNotes());

                    //2.5.1.2. 设置: 普通单元格样式
                    for (int l = 0; l < 27; l++) {
                        rowX.getCell(l).setCellStyle(cellStyle1);
                    }
                }
                //2.5.2. 合并: 合并行单元格
                //合并行  ,12-17列, 本地规则属性信息行单元格, 规则的名称,分类,致命性等属性信息(bus_rule)
                //不合并行,18-22列, 规则详细信息(item)
                //合并行  ,23-26列, 远程规则规则的创建人属性信息(Modelrulelist)
                for (int a = 12; a < 27; a++) {
                    if((a<18||a>21)&&row_num - item_list_size<row_num - 1){
                        //起行,尾行,起列,尾列
                        sheet.addMergedRegion(new CellRangeAddress(row_num - item_list_size , row_num - 1, a, a));
                    }
                }
            }
            //2.6. 合并: 合并行单元格, 0-9列, 模型名称,模型分类,创建人等信息
            if(row_num - item_sum<row_num - 1){
                for (int y = 0; y < 12; y++) {
                    sheet.addMergedRegion(new CellRangeAddress(row_num - item_sum, row_num - 1, y, y));
                }
            }
//            row_num += item_sum;
        }
        //3. 设置: 列自适应宽度, 当前POI 3.9版本,sheet.autoSizeColumn(j,true)生效,sheet.autoSizeColumn(j)不生效
        for (int j = 0; j < 27; j++) sheet.autoSizeColumn(j, true);
        return wk;
    }

    /**
     * 处理获取规则详细信息时的异常
     *
     * @param url
     * @param ruleIds
     * @return 失败:返回空数据,成功:返回规则信息
     * @throws IOException
     */
    public List<Modelrulelist> post_model_rule(String url, List<Long> ruleIds){
        try {
            String json = HttpUtil.sendJsonHttpPost(url, mapper.writeValueAsString(ruleIds));
            List<Modelrulelist> remote_rule_list = mapper.readValue(json, new TypeReference<List<Modelrulelist>>() {
            });
            return remote_rule_list;
        } catch (Exception e) {
            log.error("查询规则时异常,打印异常信息:\n{}", e);
            return new ArrayList<>();
        }
    }

    /**
     *  写一行单元格
     * @param sheet
     * @param cellStyle1
     * @param model
     * @param row_num
     * @param i
     * @return 下一行号
     */
    public int create_one_row(HSSFSheet sheet, CellStyle cellStyle1, BusModel model, int row_num, int i) {
        //创建行对象
        HSSFRow rowX = sheet.createRow(row_num);
        /*"序号", "省份", "分类", "模型名称", "模型分数","状态","测检状态", "创建人", "创建时间", "上线时间","最后更新时间", "模型备注"*/
        rowX.createCell(0).setCellValue(i + 1);
        rowX.createCell(1).setCellValue(model.getAreaname() == null ? "" : model.getAreaname());
        rowX.createCell(2).setCellValue(model.getModeltypename() == null ? "" : model.getModeltypename());
        rowX.createCell(3).setCellValue(model.getName() == null ? "" : model.getName());
        rowX.createCell(4).setCellValue(model.getFullscore() == null ? "" : model.getFullscore().toString());
        rowX.createCell(5).setCellValue(
                (model.getPoststatus()==null || model.getPoststatus() == 0) ? "未上线" : "已上线");
        rowX.createCell(6).setCellValue(
                (model.getTesttimes()==null||model.getTesttimes() == 0) ? "未测检" : "已测检");
        rowX.createCell(7).setCellValue(model.getUsername() == null ? "" : model.getUsername());
        rowX.createCell(8).setCellValue(model.getCreatetime() == null ? "" : sdf.format(model.getCreatetime()));
        rowX.createCell(9).setCellValue(model.getOnsettime() == null ? "" : sdf.format(model.getOnsettime()));
        rowX.createCell(10).setCellValue(model.getLastupdatetime() == null ? "" : sdf.format(model.getLastupdatetime()));
        rowX.createCell(11).setCellValue(model.getNote() == null ? "" : model.getNote());

        //"规则分类", "规则名称", "规则属性", "致命性", "单次扣分", "扣分上限",
        for(int ri=12;ri<18;ri++){
            rowX.createCell(ri).setCellValue("");
        }
        //"子规则序号","子规则", "子规则内容", "子规则位置"
        for(int ri=18;ri<22;ri++){
            rowX.createCell(ri).setCellValue("");
        }
        //规则其他信息，"创建人", "规则创建时间", "规则上线时间", "规则最后更新时间", "规则备注"
        for(int ri=22;ri<27;ri++){
            rowX.createCell(ri).setCellValue("");
        }
        for (int ri = 0; ri < 27; ri++) {
            rowX.getCell(ri).setCellStyle(cellStyle1);
        }
        return ++row_num;
    }

    /**
     * 写多行单元格, 根据本地规则
     * @param sheet      行对象
     * @param cellStyle1 单元格格式
     * @param model      模型对象
     * @param row_num    当前excel行号
     * @param i          模型记录数
     * @return 下一行号
     */
    public int create_multi_row(HSSFSheet sheet, CellStyle cellStyle1, BusModel model, int row_num, int i) {
        List<BusModelRule> local_rule_list = model.getModelrulelist();
        int local_rule_size = local_rule_list==null||local_rule_list.size()==0?1:local_rule_list.size();
        int rule_sum = 0;
        for (int x = 0; x < local_rule_size; x++, rule_sum++, row_num++) {
            //2.5.1.1. 控制获取规则详细信息
            HSSFRow rowX = sheet.createRow(row_num);
            /*"序号", "省份", "分类", "模型名称", "模型分数","状态","测检状态", "创建人", "创建时间", "上线时间","最后更新时间", "模型备注"*/
            rowX.createCell(0).setCellValue(i + 1);
            rowX.createCell(1).setCellValue(model.getAreaname() == null ? "" : model.getAreaname());
            rowX.createCell(2).setCellValue(model.getModeltypename() == null ? "" : model.getModeltypename());
            rowX.createCell(3).setCellValue(model.getName() == null ? "" : model.getName());
            rowX.createCell(4).setCellValue(model.getFullscore() == null ? "" : model.getFullscore().toString());
            rowX.createCell(5).setCellValue(
                    (model.getPoststatus()==null || model.getPoststatus() == 0) ? "未上线" : "已上线");
            rowX.createCell(6).setCellValue(
                    (model.getTesttimes()==null||model.getTesttimes() == 0) ? "未测检" : "已测检");
            rowX.createCell(7).setCellValue(model.getUsername() == null ? "" : model.getUsername());
            rowX.createCell(8).setCellValue(model.getCreatetime() == null ? "" : sdf.format(model.getCreatetime()));
            rowX.createCell(9).setCellValue(model.getOnsettime() == null ? "" : sdf.format(model.getOnsettime()));
            rowX.createCell(10).setCellValue(model.getLastupdatetime() == null ? "" : sdf.format(model.getLastupdatetime()));
            rowX.createCell(11).setCellValue(model.getNote() == null ? "" : model.getNote());

            //"规则分类", "规则名称", "规则属性", "致命性", "单次扣分", "扣分上限",
            if(local_rule_list!=null&&local_rule_list.get(x)!=null){
                BusModelRule modelRule =  local_rule_list.get(x);
                rowX.createCell(12).setCellValue(modelRule.getRuletypename() == null ? "" : modelRule.getRuletypename());
                rowX.createCell(13).setCellValue(modelRule.getRulename() == null ? "" : modelRule.getRulename());
                rowX.createCell(14).setCellValue(
                        (modelRule.getUserfor() == null||modelRule.getUserfor() == 0 )? "系统" : "人工");
                if(modelRule.getSignscore()==null||modelRule.getSignscore()!=1){
                    rowX.createCell(15).setCellValue(
                            modelRule.getFateful()==null||modelRule.getFateful() == 0 ? "非致命性" : "致命性");
                }else{
                    rowX.createCell(15).setCellValue("");
                }
                rowX.createCell(16).setCellValue(modelRule.getScorepertime() == null ? "" : modelRule.getScorepertime().toString());
                rowX.createCell(17).setCellValue(modelRule.getMaxscore() == null ? "" : modelRule.getMaxscore().toString());
            }else{
                for(int ri=12;ri<18;ri++){
                    rowX.createCell(ri).setCellValue("");
                }
            }
             //"子规则序号","子规则", "子规则内容", "子规则位置"
            for(int ri=18;ri<22;ri++){
                rowX.createCell(ri).setCellValue("");
            }
            //规则其他信息，"创建人", "规则创建时间", "规则上线时间", "规则最后更新时间", "规则备注"
            for(int ri=22;ri<27;ri++){
                rowX.createCell(ri).setCellValue("");
            }
            for (int ri = 0; ri < 27; ri++) {
                rowX.getCell(ri).setCellStyle(cellStyle1);
            }
        }
        for (int y = 0; y < 12; y++) {
            sheet.addMergedRegion(new CellRangeAddress(row_num - rule_sum, row_num - 1, y, y)); //起行,尾行,起列,尾列
        }
        return row_num;
    }
}
